package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomFacilityService roomFacilityService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    PaymentTypeMapper paymentTypeMapper;

    @Autowired
    LeaseTermMapper leaseTermMapper;



    /**
     * 先判断房间存不存在
     * 如果存在则更新
     * (更新)删除房间属性,删除房间配套,删除房间标签,删除支付方式,删除可选租期,删除房间图片并重新加入数据
     * 如果不存在则保存
     * @param roomSubmitVo
     */
    @Override
    public void isaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        if (!ObjectUtils.isEmpty(id)){
            //删除房间属性
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                    .eq(RoomAttrValue::getRoomId,id));
            //删除房间配套
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                    .eq(RoomFacility::getRoomId,id));
            //删除房间标签
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>()
                    .eq(RoomLabel::getRoomId,id));
            //删除房间支付方式
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                    .eq(RoomPaymentType::getRoomId,id));
            //删除房间可选租期
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                    .eq(RoomLeaseTerm::getRoomId,id));
            //删除房间图片
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId,id)
                    .eq(GraphInfo::getItemType, ItemType.ROOM));
        }
        //保存房间信息
        super.saveOrUpdate(roomSubmitVo);
        //id值回显
        id = roomSubmitVo.getId();
        //保存房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
        for (Long attrValueId : attrValueIds) {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(id).attrValueId(attrValueId).build();
            roomAttrValues.add(roomAttrValue);
        }
        roomAttrValueService.saveBatch(roomAttrValues);

        //保存房间配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            RoomFacility roomFacility = RoomFacility.builder().roomId(id).facilityId(facilityInfoId).build();
            roomFacilities.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilities);

        //保存房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        for (Long labelInfoId : labelInfoIds) {
            RoomLabel roomLabel = RoomLabel.builder().roomId(id).labelId(labelInfoId).build();
            roomLabels.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabels);

        //保存支付方式信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        for (Long paymentTypeId : paymentTypeIds) {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(id).paymentTypeId(paymentTypeId).build();
            roomPaymentTypes.add(roomPaymentType);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypes);

        //保存可选租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        for (Long leaseTermId : leaseTermIds) {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(id).leaseTermId(leaseTermId).build();
            roomLeaseTerms.add(roomLeaseTerm);
        }
        roomLeaseTermService.saveBatch(roomLeaseTerms);

        //保存房间图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemId(id);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        IPage<RoomItemVo> list = roomInfoMapper.pageItem(page,queryVo);
        return list;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //获取房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //获取所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        //获取房间属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVoList(id);
        //获取房间图片列表
        List<GraphVo> graphVoList = graphInfoMapper.getGraphVoList(id);
        //获取房间配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoList(id);
        //获取房间标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoList(id);
        //获取房间支付方式信息
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeList(id);
        //获取可选租期信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermList(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void iremoveById(Long id) {
        //删除房间信息
        super.removeById(id);
        //删除房间信息关联的图片
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemType,ItemType.ROOM)
                .eq(GraphInfo::getItemId,id));
        //删除房间信息关联的属性
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                .eq(RoomAttrValue::getRoomId,id));
        //删除房间信息关联的配套属性
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                .eq(RoomFacility::getRoomId,id));
        //删除房间信息关联的标签
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>()
                .eq(RoomLabel::getRoomId,id));
        //删除房间信息关联的支付方式
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                .eq(RoomPaymentType::getRoomId,id));
        //删除房间信息关联的可选租期
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                .eq(RoomLeaseTerm::getRoomId,id));
    }
}




